package com.shaw.client.mvc.widgt;

import com.extjs.gxt.ui.client.Registry;
import com.extjs.gxt.ui.client.Style.HorizontalAlignment;
import com.extjs.gxt.ui.client.mvc.Dispatcher;
import com.extjs.gxt.ui.client.widget.Dialog;
import com.extjs.gxt.ui.client.widget.MessageBox;
import com.extjs.gxt.ui.client.widget.Status;
import com.extjs.gxt.ui.client.widget.form.FormPanel;
import com.extjs.gxt.ui.client.widget.form.TextField;
import com.extjs.gxt.ui.client.widget.layout.FormData;
import com.extjs.gxt.ui.client.widget.layout.FormLayout;
import com.google.gwt.user.client.Element;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.shaw.client.MailServiceAsync;
import com.shaw.client.MailService.Util;
import com.shaw.client.mvc.AppEvents;
import com.extjs.gxt.ui.client.widget.button.ButtonBar;
import com.extjs.gxt.ui.client.event.Events;
import com.extjs.gxt.ui.client.event.Listener;
import com.extjs.gxt.ui.client.event.ButtonEvent;


public class LoginDialog extends Dialog {

	protected TextField<String> username;
	protected TextField<String> password;
	protected Button regist;
	protected Button login;
	protected Status status;

	protected FormPanel formPanel;
	private ButtonBar buttonBar;
	private com.extjs.gxt.ui.client.widget.button.Button buttonOK;
	private com.extjs.gxt.ui.client.widget.button.Button buttonCancel;

	public LoginDialog() {
		createContents();
	}
	private void createContents() {
		
		formPanel = new FixedFormPanel();
		formPanel.setHeaderVisible(false);
		formPanel.setBorders(false);
		formPanel.setBodyBorder(false);
		
		FormLayout layout = new FormLayout();
		formPanel.setFrame(false);
		layout.setLabelWidth(90);
		layout.setDefaultWidth(155);
		formPanel.setLayout(layout);
		add(formPanel);
		setClosable(false);
		setButtonAlign(HorizontalAlignment.LEFT);
		setHeading("请登陆");
		setModal(true);
		setBodyBorder(true);
		setBodyStyle("padding: 8px;background: none");
		setWidth(305);
		setResizable(false);
		setButtons("");
		
		username = new TextField<String>();
		//username.setValue("admin");
		username.setName("userName");
		username.setFieldLabel("用户名");
		formPanel.add(username,new FormData("-20"));
		
		password = new TextField<String>();
		//password.setValue("123");
		password.setName("passWord");
		password.setFieldLabel("密码");
		password.setPassword(true);
		formPanel.add(password,new FormData("-20"));
		
				setFocusWidget(username);
		
		this.buttonBar = new ButtonBar();
		this.buttonBar.setAlignment(HorizontalAlignment.RIGHT);
		
		this.buttonOK = new com.extjs.gxt.ui.client.widget.button.Button("确 定");
		this.buttonOK.addListener(Events.Select, new Listener<ButtonEvent>() {
			public void handleEvent(ButtonEvent e) {
				MailServiceAsync mailServiceAsync=Util.getInstance();
				mailServiceAsync.isLoginBoolean(username.getValue(), password.getValue(), new AsyncCallback<Boolean>() {
					
					@Override
					public void onSuccess(Boolean result) {
						// TODO Auto-generated method stub
							
							LoginDialog.this.hide();
							Dispatcher.get().dispatch(AppEvents.INIT);
						
					}
					
					@Override
					public void onFailure(Throwable caught) {
						// TODO Auto-generated method stub
						MessageBox.alert("登录结果", "登录失败", null);
					}
				});
				
				
				
			}
		});
		this.buttonBar.add(this.buttonOK);
		this.buttonOK.setWidth("62px");
		
		this.buttonCancel = new com.extjs.gxt.ui.client.widget.button.Button("取 消");
		this.buttonCancel.addListener(Events.Select, new Listener<ButtonEvent>() {
			public void handleEvent(ButtonEvent e) {
				username.clear();
				password.clear();
			}
		});
		this.buttonBar.add(this.buttonCancel);
		this.buttonCancel.setWidth("60px");
		add(this.buttonBar);
	}

	protected void onRender(Element parent, int pos) {
		// TODO Auto-generated method stub
		super.onRender(parent, pos);
	}

	
	/*private void onLoginSuccess(String result) {

		status.enable();
		getButtonBar().enable();
		Registry.register("person", result);
		LoginDialog.this.hide();
		Dispatcher.get().dispatch(AppEvents.INIT);
		// Dispatcher.forwardEvent(AppEvents.Init_Shortcut);
		// Dispatcher.forwardEvent(AppEvents.Init_AutoRun);
	}*/

}
